package com.mine.springmvc.run;

import sun.applet.Main;

import java.util.concurrent.*;

/*
* 通过实现Callable接口实现一个线程类
*
* 可以定义返回值
* 可以抛出异常
*
* */
public class TestCallable implements Callable {


    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i < 50; i++) {
            //模拟延迟
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName()+"-->正在听歌");
        }

        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //可以执行同一份资源，也可以执行不同的资源
        TestCallable testCallable1 = new TestCallable();
        TestCallable testCallable2 = new TestCallable();
        TestCallable testCallable3 = new TestCallable();

        //创建一个服务
        ExecutorService service = Executors.newFixedThreadPool(3);
        //提交执行
        Future<Boolean> result1 = service.submit(testCallable1);
        Future<Boolean> result2 = service.submit(testCallable1);
        Future<Boolean> result3 = service.submit(testCallable1);
        //获取结果

        Boolean flag = result1.get();
        System.out.println("执行结果"+flag);
        //关闭服务
        service.shutdownNow();


    }
}
